package com.zis.wechat.web;

import com.alibaba.fastjson.JSONObject;
import com.zis.auction.model.AuctUser;
import com.zis.auction.service.IAuctUserInter;
import com.zis.core.ResponseEntity;
import com.zis.wechat.api.inter.IWechatUserInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * Create by wangshen 2018/1/10
 */
@RestController
@RequestMapping("/api/open/wechat/user")
public class TUserRestful {

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private IAuctUserInter auctUserInter;

    @Autowired
    private IWechatUserInter wechatUserInter;


    @RequestMapping(value = "/{token}", method = RequestMethod.GET)
    @ResponseBody
    public String getUserInfo(@PathVariable("token") String token) throws Exception {
        if (token == null || token.equals("")) {
            return ResponseEntity.createErrorJsonResponse("token不能为空");
        }
        if (!redisTemplate.hasKey(token)) {
            return ResponseEntity.createNULLJsonResponse("微信授权信息不存在");
        }
        JSONObject userInfo = (JSONObject) redisTemplate.opsForValue().get(token);
        // 设置一下userId
        String tuId = userInfo.getString("tuId");
        AuctUser auctUser = auctUserInter.getAuctUserObject(tuId);
        if (auctUser != null) {
            userInfo.put("bussId", auctUser.getAusId());
            userInfo.put("authState", "1");//用户信息存在，表示已经认证
            // 重新设置到缓存
            wechatUserInter.refreshCache(userInfo);
        }
        return ResponseEntity.createNormalJsonResponse(userInfo);
    }
}
